﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;

namespace UMLTouch.Core.Entities
{
    public class Item : IEngineEntity
    {
        #region Properties

        public IEnumerable<Stroke> Strokes
        {
            get
            {
                return Shape.Segments.Select(Sh => Sh.Stroke); 
            }
        }
        public IEnumerable<Segment> Segments
        {
            get
            {
                return Shape.Segments;
            }
        }
        public IEnumerable<Intersection> Intersections
        {
            get
            {
                return Shape.Intersections;
            }
        }
        public System.Windows.Controls.Control Control {
            get
            {
                return Shape.ShapePattern.CreateControl();
            }
        }
        public Shape Shape { get; protected set; }
        public IEnumerable<System.Windows.Ink.Stroke> BaseStrokes
        {
            get
            {
                return Shape.BaseStrokes.Distinct();
            }
        } 
        
        #endregion

        #region Constructors

        public Item(Shape shape)
        {
            Shape = shape;
        } 
        
        #endregion

        #region Methods

        public void Draw(InkCanvas contextCanvas)
        {
            Draw(contextCanvas, DrawModeEnum.ControlsMode);
        }

        public void Draw(InkCanvas contextCanvas, DrawModeEnum mode)
        {
            switch (mode)
            {
                case DrawModeEnum.ControlsMode:
                    Control c = Control;
                    
                    
                    contextCanvas.Children.Add(Control);
                   
                    break;
                case DrawModeEnum.SegmentsMode:

                    var q = from segment in Segments
                            select segment.toStylusStroke();
                    foreach (var item in Intersections)
                    {
                        DrawPoint(contextCanvas, item.IntersectionPoint, Colors.Red);
                    }
                    contextCanvas.Strokes.Add(new StrokeCollection(q));
                    break;
                case DrawModeEnum.StrokeMode:
            
                    contextCanvas.Strokes.Add(new StrokeCollection(BaseStrokes));
                    break;
                //default:
                //    contextCanvas.Children.Add(Control);
                //    break;
            }
        }
        private Point DrawPoint(InkCanvas ca, Point p, Color c)
        {
            double w = 10d;
            double h = 10d;

            Ellipse ellipse = new Ellipse();

            ellipse.SetValue(InkCanvas.LeftProperty, p.X - w / 2);

            ellipse.SetValue(InkCanvas.TopProperty, p.Y - h / 2);

            ellipse.Width = w;

            ellipse.Height = h;

            ellipse.Fill = new SolidColorBrush(c);

            ca.Children.Add(ellipse);
            return p;
        }
      
        #endregion

    }
}
